<?php

/**
 * @Project NUKEVIET 3.x - GETNEWS
 * @Author ĐẶNG ĐÌNH TỨ (dlinhvan@gmail.com)
 * @Copyright (C) 2010 webdep24.com All rights reserved
 * @Createdate 10/08/2012 08:00
 */

if( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' );

if( ! defined( 'NV_IS_CRON' ) ) die( 'Stop!!!' );

function cron_auto_zing( $url, $catid, $blockcat, $sourceid )
{
	global $db, $global_config, $db_config, $module, $folder, $module_config;

	$folder = date( "Y_m" );
	$module_name = "getnews";
	$module = $module_config[$module_name]['module'];
	$mod_data = str_replace( '-', '_', $module );
	$error = "";
	$data = array();
	$link_arr = array();
	$urlink = "http://news.zing.vn";

	$loop = "div.znews_leftcol div.catelist_row";
	$title_t = "div.cate_newsexcerpt p a strong";
	$link_t = "div.cate_newsexcerpt p a";
	$home_t = "div.cate_newsexcerpt p";
	$img_t = "a img";

	require_once ( NV_ROOTDIR . "/includes/class/geturl.class.php" );
	require_once ( NV_ROOTDIR . "/modules/getnews/global.function.php" );
	$getContent = new UrlGetContents( $global_config );
	$html = $getContent->get( $url );
	$html1 = str_get_html( nv_convert( $html ) );
	if( $html1->find( $loop ) )
	{
		foreach( $html1->find( $loop ) as $e )
		{

			if( $e->find( $title_t, 0 ) ) $title = change( $e->find( $title_t, 0 )->plaintext );
			$alias = change_alias( $title );

			$data[$alias]['title'] = $title;
			$data[$alias]['alias'] = $alias;

			if( $e->find( $link_t, 0 ) ) $data[$alias]['link'] = $e->find( $link_t, 0 )->href;

			if( $e->find( $img_t, 0 ) )
			{
				$img = $e->find( $img_t, 0 )->src;
			}
			else  $img = "";
			$data[$alias]['homefile'] = $img;

			if( $e->find( $home_t, 1 ) )
			{
				$data[$alias]['hometext'] = change( $e->find( $home_t, 1 )->innertext );
				$fil = '(Zing)';
				$fil2 = substr( $data[$alias]['hometext'], 0, 6 );
				if( $fil2 == $fil )
				{
					$data[$alias]['hometext'] = substr( $data[$alias]['hometext'], 9 );
				}
			}
		}
		$html1->clear();
		unset( $html1, $html );
	}
	else
	{
		$error .= "Dữ liệu nguồn bị thay đổi hoặc link chuyên mục không đúng";
		break;
	}
	$array_block_cat_module = array();
	$sql = "SELECT bid, adddefault, title FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_block_cat` ORDER BY `weight` ASC";
	$result = $db->sql_query( $sql );
	while( list( $bid_i, $adddefault_i, $title_i ) = $db->sql_fetchrow( $result ) )
	{
		$array_block_cat_module[$bid_i] = $title_i;
	}
	$ii = 1;
	foreach( $data as $alias => $array )
	{
		$sql = "SELECT COUNT(*) FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_rows` WHERE `alias`=" . $db->dbescape_string( $alias );
		$result = $db->sql_query( $sql );
		list( $exist ) = $db->sql_fetchrow( $result );
		if( empty( $exist ) )
		{
			$html = $getContent->get( $array['link'] );
			$html1 = str_get_html( $html );
			if( ! ( $html1->find( 'div#content_document', 0 ) ) )
			{
				$error .= "Dữ liệu nguồn bị thay đổi";
				$link_arr[] = $array['link'];
			}
			else
			{
				// thoi gian
				$publtime = 0;
				$time1 = $html1->find( 'p[class="local_timer fr"]', 0 )->plaintext;
				$time2 = explode( 'GMT', $time1 );

				if( ! empty( $time1 ) )
				{
					$time = nv_time( nv_conver_utf8( $time2[0] ) );
				}
				if( ! empty( $time ) )
				{
					$emin = substr( $time, -2, 2 ) - 10;
					$ehour1 = substr( substr( $time, 0, -2 ), 6 );
					if( $ehour1 < 10 ) $ehour = "0" . $ehour1;
					else  $ehour = $ehour1;
					$y = substr( $time, 4, 2 );
					$m = substr( $time, 2, 2 );
					$d = substr( $time, 0, 2 );
					$publtime = mktime( $ehour, $emin, 0, $m, $d, "20" . $y );
					if( date( 'Y', $publtime ) == 1970 or date( 'Y', $publtime ) == 1999 )
					{
						$error .= "Lỗi Không Cập Nhật Được Thời Gian";
						break;
					}
				}
				else
				{
					$error .= "Lỗi Không Cập Nhật Được Thời Gian";
					break;
				}

				// loc chi tiet
				if( empty( $error ) )
				{
					if( strpos( $html1, '>>' ) ) $html1->find( '.newsdetail_wrapper p[class="pBody"]', 0 )->outertext = "";
					$html1->find( '.newsdetail_wrapper h1.pTitle', 0 )->outertext = "";
					$html1->find( '.newsdetail_wrapper h2[class="pHead"]', 0 )->outertext = "";
					$html1->find( '.newsdetail_wrapper .more_news', 0 )->outertext = "";
					$html1->find( '.newsdetail_wrapper .followZinglive', 0 )->outertext = "";
					$html1->find( 'p.pSource', 0 )->outertext = "";
					$html1->find( 'p.pAuthor', 0 )->outertext = "";
					$body = $html1->find( 'div#content_document', 0 )->innertext;
					$bodytext = preg_replace( "'<script[^>]*>.*</script>'siU", '', $body );
					$bodytext = url_link( $bodytext, $urlink );
					if( $module_config['getnews']['load_image'] == 1 )
					{
						$bodytext = load_image( $bodytext, '' );
					}
					$bodytext = stripwhitespace( $bodytext );

					if( ! empty( $bodytext ) )
					{
						$imgfile = nv_get_images( $bodytext );
						if( ! empty( $imgfile ) and $module_config[$module_name]['image_body'] == 1 )
						{
							$img = explode( "/", $imgfile );
							$imgfile = end( $img );
							$imgfile = NV_UPLOADS_REAL_DIR . "/" . $module . "/" . $folder . "/" . $imgfile;
							if( file_exists( $imgfile ) )
							{
								$img_file = nv_filter( $imgfile );
								$homefile = $img_file[0];
								$homeimgthumb = $img_file[1];
							}
							elseif( ! empty( $array['homefile'] ) )
							{
								$homefile = $array['homefile'];
								$homeimgthumb = "";
							}
							elseif( ! empty( $imgfile ) )
							{
								$homefile = "";
								$homeimgthumb = "";
							}
						}
						elseif( ! empty( $imgfile ) )
						{
							$homefile = $imgfile;
							$homeimgthumb = "";
						}
						elseif( $array['homefile'] != "" )
						{
							$homefile = $array['homefile'];
							$homeimgthumb = "";

						}
						else
						{
							$homefile = "";
							$homeimgthumb = "";
						}

					}
					else
					{
						$error .= "Lỗi chi tiết bài viết trống";
						break;
					}
					if( empty( $error ) )
					{
						$error .= save_data_auto( $catid, $blockcat, $array['title'], $array['alias'], $array['hometext'], $bodytext, $sourceid, $publtime, $homefile, $homeimgthumb, $array['link'], $array_block_cat_module );
					}
					$html1->clear();
					unset( $html1, $html, $bodytext );
				}
			}
		}
		++$ii;
		if( $ii > 10 ) break;
	}
	if( ! empty( $error ) )
	{
		nv_sendmail( array( $global_config['site_name'], $global_config['site_email'] ), $global_config['error_send_email'], "Error get new :" . $error, "Error get new :" . $error . " từ website " . NV_SERVER_NAME . " nguồn " . $url . "  " . implode( "<br />", $link_arr ) . "  " . $global_config['site_email'] );
	}
	else
	{
		optimize();
		nv_del_moduleCache( $module );
	}
	return true;
}

?>